import request from './request'
import storage from './storage'
module.exports = {
	share: function(data) {
		this.wxJs(['onMenuShareAppMessage', 'onMenuShareTimeline']).then(function(){
			wx.ready(function() {
				//分享到朋友
				wx.onMenuShareAppMessage({
					title: data.title, // 分享标题
					desc: data.desc, // 分享描述
					link: data.link, // 分享链接
					imgUrl: data.imgUrl, // 分享图标
					type: 'link', // 分享类型,music、video或link，不填默认为link
					success: function() {
						//alert("分享成功");
					},
					cancel: function() {
						//alert("未分享!");
					}
				});
				wx.onMenuShareTimeline({
					title: data.title, // 分享标题
					link: data.link, // 分享链接
					imgUrl: data.imgUrl, // 分享图标
					trigger: function(res) {},
					success: function(res) {},
					cancel: function(res) {},
					fail: function(res) {}
				});
			});
		})
	},
	wxJs: function(list) {
		return new Promise((succ, error) => {
			//let url = encodeURIComponent(window.location.href.split('#')[0]);
			request.post('/api/shop/common/wxjs',{url:window.location.href.split('#')[0]},'form').then(res => {
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: res.data.appId, // 必填，公众号的唯一标识
					timestamp: res.data.timestamp, // 必填，生成签名的时间戳
					nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
					signature: res.data.signature, // 必填，签名
					jsApiList: list // 必填，需要使用的JS接口列表
				});
				succ.call(self)
			})
		})
	},
	JsonToFormParmas: function (data, key = '', type = -1) {
	                    var params = [];
	                    for (var key1 in data) {
	                        var val1 = data[key1];
	                        if (Array.isArray(val1) == true) {
	                            for (var key2 in val1) {
	                                var val2 = val1[key2];
	                                if (Array.isArray(val2) == true) {
	                                    if (type == 1)
	                                        params = params.concat(this.JsonToFormParmas(val2, `${key}[${key1}][${key2}]`, 1));
	                                    else
	                                        params = params.concat(this.JsonToFormParmas(val2, `${key}${key1}[${key2}]`, 1));
	                                }
	                                else if (typeof val2 === 'object') {
	                                    if (type == 1)
	                                        params = params.concat(this.JsonToFormParmas(val2, `${key}[${key1}][${key2}]`, 1));
	                                    else
	                                        params = params.concat(this.JsonToFormParmas(val2, `${key}${key1}[${key2}]`, 1));
	                                }
	                                else {
	                                    if (type == 1) {
	                                        params.push({ key: `${key}[${key1}][]`, val: val1[key2] });
	                                    }
	                                    else {
	                                        params.push({ key: `${key}${key1}[]`, val: val1[key2] });
	                                    }
	                                }
	                            }
	                        }
	                        else if (typeof val1 === 'object') {
	                            if (type == 1)
	                                params = params.concat(this.JsonToFormParmas(val1, `${key}[${key1}]`, 1));
	                            else
	                                params = params.concat(this.JsonToFormParmas(val1, `${key}${key1}`, 1));
	                        }
	                        else {
	                            if (type == 1) {
	                                params.push({ key: key + `[${key1}]`, val: val1 });
	                            }
	                            else {
	                                params.push({ key: key1, val: val1 });
	                            }
	                        }
	                    }
	                    return params;
	                },
		GetNumberOfDays:function(date1,date2){//获得天数
		    //date1：开始日期，date2结束日期
		    var a1 = Date.parse(new Date(date1));
		    var a2 = Date.parse(new Date(date2));
		    var day = parseInt((a2-a1)/ (1000 * 60 * 60 * 24));//核心：时间戳相减，然后除以天数
		    return day
		}
}